package com.mflex.mwms.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.mwms.R;
import com.mflex.mwms.adapter.MoveTaskAdapter;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.Inventoryproduct;
import com.mflex.mwms.model.MoveTaskRequest;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.CollectionUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MoveTaskActivity extends BaseActivity {

    @BindView(R.id.stv_Warehouse)
    EditText stvWarehouse;

    @BindView(R.id.stv_BoxNumber)
    EditText stvBoxNumber;
    @BindView(R.id.stv_qty)
    EditText stvQty;

    @BindView(R.id.button_clear)
    Button buttonClear;

    @BindView(R.id.button_submit)
    Button buttonSubmit;

    @BindView(R.id.lvInventoryproductList)
    ListView lvInventoryproductList;

    MiniLoadingDialog mMiniLoadingDialog;

    List<Inventoryproduct> inventoryproductList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move_task);
        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        inventoryproductList = new ArrayList<>();
        MoveTaskAdapter moveOnShelfOnTaskAdapter = new MoveTaskAdapter(this, inventoryproductList);
        lvInventoryproductList.setAdapter(moveOnShelfOnTaskAdapter);

        stvQty.setFocusable(false);
        stvWarehouse.setFocusable(false);
        stvWarehouse.setText(MMKVUtils.getString("Warehose", ""));

        stvBoxNumber.setOnKeyListener(this);
        buttonClear.setOnClickListener(this);
        buttonSubmit.setOnClickListener(this);
        stvBoxNumber.requestFocus();

//        requestTask(this.getCurrentFocus());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                clearButtonMd(view);
                break;
            case R.id.button_submit:
                submitButtonMd(view);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_Warehouse:
//                    checkWarehouse(view);
                    break;
                case R.id.stv_BoxNumber:
                    requestCheckBox(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void requestTask(View v) {
        if (CollectionUtils.isEmpty(inventoryproductList)) {
//            new MaterialDialog.Builder(v.getContext())
//                    .iconRes(R.drawable.icon_warning)
//                    .title(R.string.tip_warning)
//                    .content("箱列表为！")
//                    .positiveText(R.string.lab_submit)
//                    .show();
            MessageUtils.errorMsgAndVoice(MoveTaskActivity.this, "箱列表为！");
            return;
        }

        MoveTaskRequest moveTaskRequest = new MoveTaskRequest();
        moveTaskRequest.setFromWH(stvWarehouse.getText().toString());
        List<String> boxList = new ArrayList<>();
        for (Inventoryproduct inventoryproduct : inventoryproductList) {
            boxList.add(inventoryproduct.getBoxNumber());
        }
        moveTaskRequest.setBoxList(boxList);

        mMiniLoadingDialog.show();
        XHttp.post("api/Task/MoveTask")
                .upJson(JsonUtil.toJson(moveTaskRequest))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String response) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxNumber.setText("");
                        stvQty.setText("");
                        stvBoxNumber.requestFocus();
                        inventoryproductList = new ArrayList<>();
                        MoveTaskAdapter moveTaskAdapter = new MoveTaskAdapter(MoveTaskActivity.this, inventoryproductList);
                        lvInventoryproductList.setAdapter(moveTaskAdapter);
                        XToastUtils.success("提交成功", 3000);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(MoveTaskActivity.this, e);
                    }
                }) {
                });
    }

    private void requestCheckBox(View v) {
        boolean existFlag = false;
        for (Inventoryproduct inventoryproduct : inventoryproductList) {
            if (StringUtils.equals(stvBoxNumber.getText().toString(), inventoryproduct.getBoxNumber())) {
                existFlag = true;
                break;
            }
        }
        if (existFlag) {
//            new MaterialDialog.Builder(v.getContext())
//                    .iconRes(R.drawable.icon_warning)
//                    .title(R.string.tip_warning)
//                    .content("箱号已在列表中")
//                    .positiveText(R.string.lab_submit)
//                    .show();
            MessageUtils.errorMsgAndVoice(MoveTaskActivity.this, "箱号已在列表中");
            stvBoxNumber.setText("");
            stvBoxNumber.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stvBoxNumber.requestFocus();
                }
            },200);
            return;
        }

        mMiniLoadingDialog.show();
        XHttp.get("api/Task/GetMoveInventoryForPda")
                .params("BoxNumber", stvBoxNumber.getText().toString())
                .params("CurrentWarehouse", MMKVUtils.getString("Warehose", ""))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<Inventoryproduct>, Inventoryproduct>(new SimpleCallBack<Inventoryproduct>() {

                    @Override
                    public void onSuccess(Inventoryproduct response) {
                        mMiniLoadingDialog.dismiss();
                        inventoryproductList.add(response);
                        MoveTaskAdapter moveTaskAdapter = new MoveTaskAdapter(MoveTaskActivity.this, inventoryproductList);
                        lvInventoryproductList.setAdapter(moveTaskAdapter);
                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();
                        stvQty.setText(String.valueOf(inventoryproductList.size()));
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(MoveTaskActivity.this, e);
                    }
                }) {
                });
    }

    private void clearButtonMd(View v) {
        stvBoxNumber.setText("");
        stvQty.setText("");
        stvBoxNumber.requestFocus();
        inventoryproductList = new ArrayList<>();
        MoveTaskAdapter moveTaskAdapter = new MoveTaskAdapter(MoveTaskActivity.this, inventoryproductList);
        lvInventoryproductList.setAdapter(moveTaskAdapter);
        XToastUtils.success("重置成功！", 3000);
    }

    private void submitButtonMd(View v) {
        requestTask(v);
    }

}